package com.zhengsr.latte.delegates;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation_swipeback.SwipeBackFragment;

/**
 * Created by zhengshaorui on 2018/3/7.
 * 基类的fragment
 */

public abstract class BaseDelegate extends SwipeBackFragment {
    private Unbinder mUnbinder = null;

    public abstract Object getLayout();
    public abstract void initView(@Nullable Bundle savedInstanceState,View view);
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = null;
        if (getLayout() instanceof Integer){
            rootView = LayoutInflater.from(getContext()).inflate((Integer) getLayout(),container,false);
        }else if(getLayout() instanceof View){
            rootView = (View) getLayout();
        }
        if (rootView != null){
            //绑定 butterknife
            mUnbinder = ButterKnife.bind(this,rootView);
            initView(savedInstanceState,rootView);
        }
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mUnbinder != null){
            mUnbinder.unbind();
        }
    }
}
